import Ember from 'ember';
import Changeset from 'ember-changeset';
import StaffValidations from '../../../validations/staff';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(StaffValidations, {
    pathConfiger: Ember.inject.service("path-configer"),
    refreshPageNumbers: true,
    showPopModal: false,
    staffModel: Ember.computed("staff", function() {
        var model = this.get("staff");
        console.log('staffModelMMMM   ' + model);
        if (!model) {
            return null;
        }
        return new Changeset(model, lookupValidator(StaffValidations), StaffValidations);
    }),
    mainController: Ember.inject.controller('business.mainpage'),
    uploadUrl: Ember.computed('property', function() {
        return this.get("pathConfiger").get("uploadUrl");
    }),
    deformation: function() {
        if ($(".cccc >div img").width() >= $(".cccc >div img").height()) {
            $(".cccc >div ").addClass("intergration_normal");
            $(".cccc >div img").css("height", "110px  ");
            $(".cccc >div img").css("width", "auto");
            console.log("this.widthcccccccccccccc==========", $(".cccc >div img").width());
            console.log('==========cccccccccccccccintergration_normal_height', $(".cccc >div img").height(), $(".cccc >div img").width());
        } else {
            $(".cccc >div ").addClass("intergration_normal");
            $(".cccc >div img").css("width", "110px ");
            $(".cccc >div img").css("height", "auto");
            console.log("this.heightccccc==========", $(".cccc >div img").height());
            console.log('==========cccccccccccccccintergration_normal', $(".cccc >div img").height(), $(".cccc >div img").width());
        }
    },
    editModel: null,
    /*editFlag: Ember.computed(function () {
      //console.log("isMobile in:", this.get("global_curStatus").get("isMobile"));
      if(this.editModel){
        console.log("editFlag1111");
        return true;
      }else{
        console.log("editFlag0000");
        return false;
      }
    }),*/
    /*init() {
      this._super(...arguments);
      let model = this.get("store").createRecord('staff', {});
      this.staff = model;
    },*/
    refreshStaffList: function() {
        var route = App.lookup('route:business.mainpage.staff-management');
        // route.doQuery();
        route.refresh();
    },
    constants: Constants,
    actions: {
        invalid() {
            //alert("error");
        },
        saveClick: function() {
            //alert("save");
            var _self = this;
            var staffModel = this.get("staffModel");
            var staff = this.get("staff");
            staffModel.validate().then(function() {
                //alert("save   out");
                if (staffModel.get('errors.length') === 0) {
                    var sysPas = _self.get("sysPassWord");
                    var appPas = _self.get("appPassWord");
                    console.log("password  " + sysPas + " " + appPas);
                    if (appPas && appPas !== '') {
                        staffModel.set('appPassWord', $.md5(appPas));
                    }
                    if (sysPas && sysPas !== '') {
                        staffModel.set('passcode', $.md5(sysPas));
                    }
                    App.lookup('controller:business.mainpage').openPopTip("正在保存");
                    staffModel.save().then(function(staff) {
                        if (staff.get('errcode') === 0) {
                            //  _self.get("staffModel").addError('loginName', '登录名已存在');
                            App.lookup('controller:business.mainpage').showPopTip("登录名已存在");
                            return false;
                        } else if (staff.get('errcode') === 1) {
                            //   _self.get("staffModel").addError('staffCardCode', '该身份证号已注册');
                            App.lookup('controller:business.mainpage').showPopTip("该身份证号已注册");
                            return false;
                        } else {
                            if (_self.get('operateFlag') == 'edit') {
                                console.log("edit   detailSaveClick");
                                _self.set('editModel', null);
                                /*_self.get("mainController").switchMainPage('staff-management', {
                                    flag: 'edit'
                                });*/
                                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                            }
                            if (_self.get('operateFlag') == 'add') {
                                console.log("add   detailSaveClick");
                                //  _self.set('staffModel', null);
                                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                                _self.get("mainController").switchMainPage('staff-management', {
                                    flag: 'add'
                                });
                            }
                        }
                    });
                } else {
                    staffModel.set("validFlag", Math.random());
                }
            });
        },
        editStaff: function() {
            var _self = this;
            console.log("id  edit " + _self.get('id'));
            this.store.findRecord('user', _self.get('id')).then(function(staff) {
                staff.set('staffContactRelation', _self.get("staffContactRelation"));
                staff.set('staffEducation', _self.get("staffEducation"));
                staff.set('staffSex', _self.get("staffSex"));
                staff.set('role', _self.get("role"));
                //staff.set('privilege',_self.get("privilege"));
                // staff.set('org',_self.get("org"));
                staff.set('hireType', _self.get("hireType"));
                console.log("TEST   " + _self.get('staffEducation').get('typename') + " " + _self.get('staffSex').get('typename'));
                var sysPas = _self.get("staff").get("sysPassWord");
                var appPas = _self.get("staff").get("appPassWord");
                if (appPas !== '') {
                    staff.set('appPassWord', $.md5(appPas));
                }
                if (sysPas !== '') {
                    staff.set('sysPassWord', $.md5(sysPas));
                }
                staff.set('delStatus', 0);
                _self.set('staff', staff);
                console.log(" staffModel  " + _self.get('staffModel'));
                var staffModel = this.get("staffModel");
                staffModel.validate().then(function() {
                    if (staffModel.get('errors.length') === 0) {
                        staffModel.save().then(function() {
                            _self.set('editModel', null);
                            //  _self.set('staffModel', null);
                            _self.get("mainController").switchMainPage('staff-management', {
                                flag: 'edit'
                            });
                        });
                    } else {

                    }
                });
            });
        },
        addStaff: function() {
            var _self = this;
            var staff = _self.store.createRecord('user', {});
            staff.set('staffContactRelation', _self.get("staffContactRelation"));
            staff.set('staffEducation', _self.get("staffEducation"));
            staff.set('staffSex', _self.get("staffSex"));
            staff.set('role', _self.get("role"));
            //staff.set('privilege',_self.get("privilege"));
            // staff.set('org',_self.get("org"));
            staff.set('hireType', _self.get("hireType"));
            staff.set('staffName', _self.get("staffName"));
            staff.set('showName', _self.get("showName"));
            staff.set('curAddress', _self.get("curAddress"));
            staff.set('hbeaconID', _self.get("hbeaconID"));
            staff.set('profession', _self.get("profession"));
            staff.set('staffTel', _self.get("staffTel"));
            staff.set('staffCardCode', _self.get("staffCardCode"));
            staff.set('staffCensus', _self.get("staffCensus"));
            staff.set('certificateName', _self.get("certificateName"));
            staff.set('staffContactName', _self.get("staffContactName"));
            staff.set('staffContactTel', _self.get("staffContactTel"));
            staff.set('staffMail', _self.get("staffMail"));
            staff.set('staffNation', _self.get("staffNation"));
            staff.set('remark', _self.get("remark"));
            staff.set('schoolName', _self.get("schoolName"));
            var sysPas = _self.get("sysPassWord");
            var appPas = _self.get("appPassWord");
            if (appPas !== '') {
                staff.set('appPassWord', $.md5(appPas));
            }
            if (sysPas !== '') {
                staff.set('sysPassWord', $.md5(sysPas));
            }
            staff.set('delStatus', 0);
            _self.set('staff', staff);

            var staffModel = this.get("staffModel");
            staffModel.validate().then(function() {
                if (staffModel.get('errors.length') === 0) {
                    staffModel.save().then(function() {
                        _self.set('staffModel', null);
                        _self.get("mainController").switchMainPage('staff-management', {
                            flag: 'add'
                        });
                    });
                } else {

                }
            });
        },
        cancelOperate: function() {
            console.log("cancelEdit  " + this.get('id'));
            if (this.get('operateFlag') == 'edit') {
                this.get("staff").rollbackAttributes();
                this.set("staffModel", new Changeset(this.get("staff"), lookupValidator(StaffValidations), StaffValidations));

            }
            this.get("mainController").switchMainPage('staff-management', {
                flag: 'edit-add'
            });
        },

        //删除按钮
        delById: function() {
            //  this.set('showpopInvitePassModal',true);
            var _self = this;
            App.lookup('controller:business.mainpage').showConfirm("是否确定删除此记录", function() {
                _self.delStaff();
            });
        },
        //弹窗取消
        invitation() {
            this.set('showpopInvitePassModal', false);
        },

        /*删除*/
        delStaff: function() {
            var _self = this;
            /*this.store.findRecord('staff', _self.get('id')).then(function(staff) {
                staff.set("delStatus", 1);
                staff.save().then(function() {
                    _self.get("mainController").switchMainPage('staff-management', {});
                });
            });*/

            var staffT = this.store.peekRecord("user", _self.get("id"));
            staffT.set("delStatus", 1);
            //通过增加计数来触发分页条刷新
            App.lookup('controller:business.mainpage').openPopTip("正在删除");
            staffT.save().then(function() {
                _self.refreshStaffList();
                App.lookup('controller:business.mainpage').showPopTip("删除成功");
                var mainpageController = App.lookup('controller:business.mainpage');
                mainpageController.switchMainPage('staff-management');
            });
        },
        editModelModify: function() {
            this.set('editModel', 1);
        },
        cancelEdit: function() {
            this.set('editModel', null);
            console.log("name  " + this.get("staff").get("staffName") + " " + this.get("staffModel").get("staffName"));
            this.get("staff").rollbackAttributes();
            this.set("staffModel", new Changeset(this.get("staff"), lookupValidator(StaffValidations), StaffValidations));
        },
        sexSelect: function(dict) {
            this.set("staff.staffSex", dict);
            this.set('staffModel.staffSex', dict);
        },
        hireTypeSelect: function(dict) {
            this.set("staff.hireType", dict);
            this.set('staffModel.hireType', dict);
        },
        educationSelect: function(dict) {
            this.set("staff.staffEducation", dict);
            this.set('staffModel.staffEducation', dict);
        },
        relationTypeSelect: function(dict) {
            this.set("staff.staffContactRelation", dict);
            this.set('staffModel.staffContactRelation', dict);
        },
        roleSelect: function(role) {
            this.set("staff.role", role);
            this.set('staffModel.role', role);
        },
        nationalitySelect: function(dict) {
            this.set("staff.staffNation", dict);
            this.set('staffModel.staffNation', dict);
        },
        nativeSelect: function(dict) {
            this.set("staff.staffCensus", dict);
            this.set('staffModel.staffCensus', dict);
        },
    }
});
